/*
 * 作者: junmu
 * 时间: 2025-07-22
 */
package com.hzbc.faceattendance.controller;

import com.hzbc.faceattendance.dto.ApiResponse;
import com.hzbc.faceattendance.dto.AttendanceQueryRequest;
import com.hzbc.faceattendance.model.AttendanceRecord;
import com.hzbc.faceattendance.service.IAttendanceService;
import com.hzbc.faceattendance.utils.DateRangeValidator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Tag(name = "考勤管理", description = "考勤记录相关接口")
@RestController
@RequestMapping("/api/attendance")
public class AttendanceController {

    @Autowired
    private IAttendanceService attendanceService;

    @Operation(summary = "获取所有考勤记录", description = "根据日期范围和关键词分页获取所有考勤记录")
    @PostMapping("/all")
    public ApiResponse<Page<AttendanceRecord>> getAllRecords(
            @RequestBody AttendanceQueryRequest request,
            @PageableDefault(size = 20) Pageable pageable) {
        ApiResponse<?> errorResponse = DateRangeValidator.validateForResponse(request);
        Date[] dates = DateRangeValidator.validateAndParse(request);
        if (errorResponse != null) {
            return (ApiResponse<Page<AttendanceRecord>>) errorResponse;
        }
        if(request.getStatus() != null) {
            if (request.getStatus() == 0) {
                Page<AttendanceRecord> records = attendanceService.getEnteredRecords(dates[0], dates[1], request.getKeyword(), pageable);
                return new ApiResponse<>(true, 0, "获取所有考勤记录成功", records);
            } else if (request.getStatus() == 1) {
                Page<AttendanceRecord> records = attendanceService.getLeftRecords(request.getKeyword(), dates[0], dates[1], pageable);
                return new ApiResponse<>(true, 0, "获取所有考勤记录成功", records);
            }
        }
        Page<AttendanceRecord> records = attendanceService.getAllRecords( dates[0], dates[1], request.getKeyword(), pageable);
        return new ApiResponse<>(true, 0, "获取所有考勤记录成功", records);
    }

    @Operation(summary = "获取已进入记录")
    @PostMapping("/entered")
    public ApiResponse<Page<AttendanceRecord>> getEnteredRecords(
            @RequestBody AttendanceQueryRequest request,
            @PageableDefault(size = 20) Pageable pageable) {
        ApiResponse<?> errorResponse = DateRangeValidator.validateForResponse(request);
        if (errorResponse != null) {
            return (ApiResponse<Page<AttendanceRecord>>) errorResponse;
        }

        Date[] dates = DateRangeValidator.validateAndParse(request);
        Page<AttendanceRecord> records = attendanceService.getEnteredRecords(dates[0], dates[1],request.getKeyword(), pageable);
        return new ApiResponse<>(true, 0, "获取已进入记录成功", records);
    }

    @Operation(summary = "获取已离开记录")
    @PostMapping("/left")
    public ApiResponse<Page<AttendanceRecord>> getLeftRecords(
            @RequestBody AttendanceQueryRequest request,
            @PageableDefault(size = 20) Pageable pageable) {
        ApiResponse<?> errorResponse = DateRangeValidator.validateForResponse(request);
        if (errorResponse != null) {
            return (ApiResponse<Page<AttendanceRecord>>) errorResponse;
        }

        Date[] dates = DateRangeValidator.validateAndParse(request);
        Page<AttendanceRecord> records = attendanceService.getLeftRecords(request.getKeyword(),dates[0], dates[1], pageable);
        return new ApiResponse<>(true, 0, "获取已离开记录成功", records);
    }

//    @Operation(summary = "获取缺席记录")
//    @PostMapping("/absent")
//    public ApiResponse<List<User>> getAbsentRecords(
//            @RequestBody AttendanceQueryRequest request) {
//        Date date = request.getStartDate() != null ? DateTimeUtil.parse(request.getStartDate()) : new Date();
//        List<User> users = attendanceService.getAbsentRecords(date, request.getClassId());
//        return new ApiResponse<>(true, 0, "获取缺席记录成功", users);
//    }

    @Operation(summary = "导出考勤报表")
    @GetMapping("/export")
    public ApiResponse<byte[]> exportAttendanceReport(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        try {
            byte[] reportBytes = attendanceService.exportAttendanceReport(date);

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "attendance_" + date + ".xlsx");

            return new ApiResponse<>(true, 0, "导出考勤报表成功", reportBytes);
        } catch (Exception e) {
            return new ApiResponse<>(false, 1, "导出考勤报表失败: " + e.getMessage(), null);
        }
    }
}